<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span id="flowsetname"></span><div class="move_title"></div><?php include "../public/subturn.php";?></div></div>
    <div class="main-container">

        <div class="block-generic">
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">申请人</label>
                        <div class="layui-input-block">
                            <input type="text" id="flowuser" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">申请部门</label>
                        <div class="layui-input-block">
                            <input type="text" id="flowdept" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>项目名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="enginid" class="layui-input enginid" onclick="openPage('../public/choose_engin.php?dom=enginid','80%','80%')">
                            <div class="choose_div" id="enginid" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_engin.php?dom=enginid','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">变更单号</label>
                        <div class="layui-input-block">
                            <input type="text" id="number" placeholder="请输入" readonly autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>变更主题</label>
                        <div class="layui-input-block">
                            <input type="text" id="zhuname" name="zhuname" placeholder="请输入" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>预算主题</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="budgetid" class="layui-input budgetid" onclick="choose_engin_budget()">
                            <div class="choose_div" id="budgetid" key="1"></div>
                            <span class="choose_one" onclick="choose_engin_budget()"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>变更类型</label>
                        <div class="layui-input-block">
                            <select id="type" name="type" lay-filter="type" >
                                <option value="1">调整</option>
                                <option value="2">增项</option>
                                <option value="3">调整及增项</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">关联单据</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="flowrelas" class="layui-input flowrelas" onclick="openPage('../public/choose_flowrelas.php?dom=flowrelas','80%','80%');">
                            <div class="choose_div" id="flowrelas" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_flowrelas.php?dom=flowrelas','80%','80%');"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-hide" id="advanersDiv">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">预审人</label>
                        <div class="layui-input-block relative">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=advaners&type=user&single=2','770px','490px');"></div>
                                <div class="tanxiannei" id="advaners"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=advaners&type=user&single=2','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-hide" id="nexterDiv">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>指定下一步审核人</label>
                        <div class="layui-input-block relative">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=nexter&type=user&single=1','770px','490px');"></div>
                                <div class="tanxiannei" id="nexter"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=nexter&type=user&single=1','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include '../public/upinfo.php'?>
        </div>


        <div class="block-generic" id="tiaoDiv">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>调整明细</span></div>
                        <div>
                            <button type="button" onclick="choose_engin_budget_list()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">从预算明细选择</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th>成本科目</th>
                            <th>名称及规格</th>
                            <th>单位</th>
                            <th>预算数量</th>
                            <th>预算单价(元)</th>
                            <th>预算金额(元)</th>
                            <th><text>*</text>变更后数量</th>
                            <th><text>*</text>变更后单价(元)</th>
                            <th><text>*</text>变更后金额(元)</th>
                            <th>变更原因</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="tiao_list"></tbody>
                    </table>
                    <div class="listerror" id="tiao_listerror">提示：<span></span></div>
                </div>
            </div>
        </div>

        <div class="block-generic layui-hide" id="zengDiv">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="2">
                    <div class="opt-btn-group">
                        <div><span>增项明细</span></div>
                        <div>
                            <button type="button" onclick="ctrlv_data_open()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">粘贴新增</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_copy_row">复制行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>成本科目</th>
                            <th><text>*</text>名称及规格</th>
                            <th><text>*</text>单位</th>
                            <th><text>*</text>预算数量</th>
                            <th><text>*</text>预算单价(元)</th>
                            <th><text>*</text>预算金额(元)</th>
                            <th>说明</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="zeng_list"></tbody>
                    </table>
                    <div class="listerror" id="zeng_listerror">提示：<span></span></div>
                </div>
            </div>
        </div>


    </div>
</form>
<script>
    var param_budgetid = getQueryVariable('budgetid') || 0;

    $("#commentForm").validate({
        rules: {
            zhuname: {required: true},
        },
        messages:{
            zhuname: {required: "请输入"},
        }
    });


    function choose_engin_budget(){
        var enginid = get_choose_val('enginid');
        if(!enginid){
            facade.error("请先选择项目");return false;
        }
        openPage('../public/choose_engin_budget.php?dom=budgetid&page=engin_budbian&enginid='+enginid,'80%','80%')
    }

    function choose_engin_budget_list(obj){
        var budgetid = get_choose_val('budgetid');
        if(!budgetid){
            facade.error("请先选择预算主题");return false;
        }
        openPage('../public/choose_engin_budget_list.php?page=engin_budbian&budgetid='+budgetid,'80%','80%');
    }


    function choose_budsub(obj){
        var domkey = $(obj).parents('tr').index();
        openPage('../public/choose_engin_budsub.php?page=engin_budbian&domkey='+domkey,'80%','80%');
    }

    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_flowdata_add_engin_budbian, data:{budgetid:param_budgetid}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data;
                $("#flowsetname").text(data.flowsetname);
                $("#flowuser").val(data.flowuser);
                $("#flowdept").val(data.flowdept);
                if(data.isadvan){
                    $("#advanersDiv").removeClass('layui-hide');
                }
                if(data.isnext){
                    $("#nexterDiv").removeClass('layui-hide');
                }
                $("#number").val(data.number);

                var budgetinfo = res.data.budgetinfo;
                if(budgetinfo){
                    set_choose_btn('budgetid',budgetinfo.id,budgetinfo.name);
                }

                var engininfo = res.data.engininfo;
                if(engininfo){
                    set_choose_btn('enginid',engininfo.id,engininfo.name);
                }
                short_table_opt_func('',1);

                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td>';
                listAddStr += '<input type="text" onclick="choose_budsub(this)" readonly placeholder="请选择" subid="0" class="layui-input subname">';
                listAddStr += '</td>';
                listAddStr += '<td><input type="text" class="layui-input name"></td>';
                listAddStr += '<td><input type="text" class="layui-input unit"></td>';
                listAddStr += '<td><input type="number" class="layui-input num"></td>';
                listAddStr += '<td><input type="number" class="layui-input price"></td>';
                listAddStr += '<td><input type="number" disabled class="layui-input money disabled"></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr, 2);



                public_upload_file_load('upload_files');
                render_from();
                initParseMoney();
            }
        });


        form.on('select(type)', function(data){
            if(data.value == 1){
                $('#tiaoDiv').removeClass('layui-hide');
                $('#zengDiv').addClass('layui-hide');
            }
            if(data.value == 2){
                $('#tiaoDiv').addClass('layui-hide');
                $('#zengDiv').removeClass('layui-hide');
            }
            if(data.value == 3){
                $('#tiaoDiv').removeClass('layui-hide');
                $('#zengDiv').removeClass('layui-hide');
            }
        });


        form.on('submit(add)', function(data){
            data.field.isturn = $(this).attr('key') || 0;
            data.field.flowrelas = get_choose_val('flowrelas');
            data.field.advaners = tan_tags_getvalue("#advaners", 'user');
            data.field.nexter = tan_tags_getvalue("#nexter", 'user');
            data.field.enginid = get_choose_val('enginid');
            data.field.budgetid = get_choose_val('budgetid');

            if(data.field.isturn > 0){
                var validRes = $("#commentForm").valid();
                formValid('enginid');
                if(data.field.enginid == undefined || !data.field.enginid){
                    validRes = false;
                }

                formValid('budgetid');
                if(data.field.budgetid == undefined || !data.field.budgetid){
                    validRes = false;
                }
                if(!validRes){return false;}
            }

            var tiaolist = [''];
            if(data.field.type == 1 || data.field.type == 3){
                try {
                    $('#tiao_list tr').each(function (i) {
                        var listid = $(this).find('.subname').attr('listid');
                        var subid = $(this).find('.subname').attr('subid');
                        var subname = $(this).find('.subname').val();
                        var name = $(this).find('.name').val();
                        var unit = $(this).find('.unit').val();
                        var num = $(this).find('.num').val();
                        var price = $(this).find('.price').val();
                        var money = $(this).find('.money').val();
                        var new_num = $(this).find('.new_num').val();
                        var new_price = $(this).find('.new_price').val();
                        var new_money = $(this).find('.new_money').val();
                        var explain = $(this).find('.explain').val();

                        var index = i + 1;

                        if(data.field.isturn > 0){
                            if(!new_num){
                                throw new Error('第'+index+'行，请输入变更后数量');
                            }
                            if(!new_price){
                                throw new Error('第'+index+'行，请输入变更后单价');
                            }

                            var chongKey = check_tiao_chongfu(i, listid);
                            if(chongKey){
                                throw new Error('第'+chongKey+'行'+'与第'+index+'行，数据重复');
                            }
                        }


                        tiaolist.push({
                            listid:listid,
                            subid:subid,
                            subname:subname,
                            name:name,
                            unit:unit,
                            num:num,
                            price:price,
                            money:money,
                            new_num:new_num,
                            new_price:new_price,
                            new_money:new_money,
                            explain:explain,
                        });
                    });
                } catch(e) {
                    $("#tiao_listerror").find('span').text(e.message);
                    $("#tiao_listerror").show();
                    return false;
                }
                $("#tiao_listerror").hide();
            }
            data.field.tiaolist = tiaolist;


            var zenglist = [''];
            if(data.field.type == 2 || data.field.type == 3){
                try {
                    $('#zeng_list tr').each(function (i) {
                        var listid = 0;
                        var subid = $(this).find('.subname').attr('subid');
                        var subname = $(this).find('.subname').val();
                        var name = $(this).find('.name').val();
                        var unit = $(this).find('.unit').val();
                        var num = $(this).find('.num').val();
                        var price = $(this).find('.price').val();
                        var money = $(this).find('.money').val();
                        var explain = $(this).find('.explain').val();

                        var index = i + 1;

                        if(data.field.isturn > 0){
                            if(!subid){
                                throw new Error('第'+index+'行，请选择科目');
                            }
                            if(!name){
                                throw new Error('第'+index+'行，请输入名称及规格');
                            }
                            if(!unit){
                                throw new Error('第'+index+'行，请输入单位');
                            }
                            if(!num){
                                throw new Error('第'+index+'行，请输入预算数量');
                            }
                            if(!price){
                                throw new Error('第'+index+'行，请输入预算单价');
                            }

                            var chongKey = check_zeng_chongfu(i, subid, name, unit);
                            if(chongKey){
                                throw new Error('第'+chongKey+'行'+'与第'+index+'行，数据重复');
                            }
                        }



                        zenglist.push({
                            listid:listid,
                            subid:subid,
                            subname:subname,
                            name:name,
                            unit:unit,
                            num:num,
                            price:price,
                            money:money,
                            explain:explain,
                        });
                    });
                } catch(e) {
                    $("#zeng_listerror").find('span').text(e.message);
                    $("#zeng_listerror").show();
                    return false;
                }
                $("#zeng_listerror").hide();
            }
            data.field.zenglist = zenglist;


            data.field.files = public_upload_file_value('upload_files');
            data = parseMoney(data);
            public_flow_submit(data.field, ask_flowdata_add_engin_budbian, ask_flowdata_edit_engin_budbian, $(this).attr('but'));
            return false;
        });

    });



    //检测重复
    function check_tiao_chongfu(index, listid){
        try {
            $('#tiao_list tr').each(function (i) {
                var cur_listid = $(this).find('.subname').attr('listid');
                if(index != i){
                    if(listid === cur_listid){
                        throw new Error(i+1);
                    }
                }
            });
        } catch(e) {
            return e.message;
        }

        return 0;
    }

    function check_zeng_chongfu(index, subid, name, unit){
        try {
            $('#zeng_list tr').each(function (i) {
                var cur_subid = $(this).find('.subname').attr('subid');
                var cur_name = $(this).find('.name').val();
                var cur_unit = $(this).find('.unit').val();
                if(index != i){
                    if(subid === cur_subid && name === cur_name && unit === cur_unit){
                        throw new Error(i+1);
                    }
                }
            });
        } catch(e) {
            return e.message;
        }

        return 0;
    }



    $('#tiao_list').on('input propertychange','.new_num,.new_price',function () {
        auto_tiao_money();
    });
    function auto_tiao_money() {
        $('#tiao_list tr').each(function (i) {
            var new_num = parseValidNum($(this).find('.new_num').val());
            var new_price = parseValidNum($(this).find('.new_price').val());
            var new_money = (new_num * new_price).toFixed(2);
            $(this).find('.new_money').val(new_money);
        });
    }





    $('#zeng_list').on('input propertychange','.num,.price',function () {
        auto_zeng_money();
    });
    function auto_zeng_money() {
        $('#zeng_list tr').each(function (i) {
            var num = parseValidNum($(this).find('.num').val());
            var price = parseValidNum($(this).find('.price').val());
            var money = (num * price).toFixed(2);
            $(this).find('.money').val(money);
        });
    }







    var ctrlv_field_name = [
        {field: 'name', name:'名称及规格'},
        {field: 'unit', name:'单位'},
        {field: 'num', name:'预算数量',type:'number'},
        {field: 'price', name:'预算单价(元)',type:'number'},
        {field: 'explain', name:'说明'},
    ];
    function ctrlv_data_show(list) {
        if(list.length > 0){
            var nums = $("#zeng_list tr").length;
            for (var key in list){
                var str = '';
                nums++;
                str += '<tr>';
                str += '<td style="width: 45px">'+nums+'</td>';
                str += '<td>';
                str += '<input type="text" onclick="choose_budsub(this)" subid="" value="" readonly placeholder="请选择" class="layui-input subname">';
                str += '</td>';
                str += '<td><input type="text" class="layui-input name" value="'+list[key].name+'"></td>';
                str += '<td><input type="text" class="layui-input unit" value="'+list[key].unit+'"></td>';
                str += '<td><input type="number" class="layui-input num" value="'+list[key].num+'"></td>';
                str += '<td><input type="number" class="layui-input price" value="'+list[key].price+'"></td>';
                str += '<td><input type="number" disabled class="layui-input money disabled"></td>';
                str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                str += '</tr>';
                $("#zeng_list").append(str);
            }
        }
        auto_zeng_money();
        render_date();
        render_from();
    }
</script>
</body>
</html>
